package svc

import (
	"fmt"
	"jgt-oper/src/bll"
	"jgt-oper/src/context"
	"jgt-oper/src/schema"
	"jgt-oper/src/util"
)

// AuthCode 认证码管理
type AuthCode struct {
	AuthCodeBll   *bll.AuthCode   `inject:""`
	EnterpriseBll *bll.Enterprise `inject:""`
}

// QueryPage 查询分页数据
func (a *AuthCode) QueryPage(ctx *context.Context) error {
	pageIndex, pageSize := ctx.GetPageIndex(), ctx.GetPageSize()

	params := schema.AuthCodeQueryParam{
		Code:           ctx.Query("code"),
		EnterpriseID:   ctx.GetEnterpriseID(),
		CodeType:       ctx.Query("code_type"),
		EffectiveMode:  util.S(ctx.Query("effective_mode")).Int(),
		EffectiveStart: util.S(ctx.Query("effective_start")).Int64(),
		EffectiveEnd:   util.S(ctx.Query("effective_end")).Int64(),
		Status:         util.S(ctx.Query("status")).Int(),
	}

	total, items, err := a.AuthCodeBll.QueryPage(params, pageIndex, pageSize)
	if err != nil {
		return ctx.ResErrInternalServer(err)
	}

	for i := 0; i < len(items); i++ {
		eitem, _ := a.EnterpriseBll.Get(items[i].EnterpriseID)
		if eitem != nil {
			items[i].EnterpriseName = eitem.Name
		}
	}

	result := map[string]interface{}{
		"list": items,
		"pagination": map[string]interface{}{
			"total":    total,
			"current":  pageIndex,
			"pageSize": pageSize,
		},
	}

	return ctx.ResSuccess(result)
}

// QueryHistoryPage 查询历史分页数据
func (a *AuthCode) QueryHistoryPage(ctx *context.Context) error {
	pageIndex, pageSize := ctx.GetPageIndex(), ctx.GetPageSize()

	params := schema.AuthHistoryQueryParam{
		CodeID: ctx.Param("id"),
	}

	total, items, err := a.AuthCodeBll.QueryHistoryPage(params, pageIndex, pageSize)
	if err != nil {
		return ctx.ResErrInternalServer(err)
	}

	result := map[string]interface{}{
		"list": items,
		"pagination": map[string]interface{}{
			"total":    total,
			"current":  pageIndex,
			"pageSize": pageSize,
		},
	}

	return ctx.ResSuccess(result)
}

// Get 精确查询数据
func (a *AuthCode) Get(ctx *context.Context) error {
	item, err := a.AuthCodeBll.Get(ctx.Param("id"))
	if err != nil {
		return ctx.ResErrInternalServer(err)
	} else if item != nil {
		eitem, _ := a.EnterpriseBll.Get(item.EnterpriseID)
		if eitem != nil {
			item.EnterpriseName = eitem.Name
		}
	}
	return ctx.ResSuccess(item)
}

// Create 创建数据
func (a *AuthCode) Create(ctx *context.Context) error {
	var item schema.AuthCode
	if err := ctx.ParseJSON(&item); err != nil {
		return ctx.ResErrBadRequest(err)
	} else if ctx.GetUserType() != "40" {
		return ctx.ResErrBadRequest(fmt.Errorf("没有创建认证码的权限"))
	}

	item.EnterpriseID = ctx.GetEnterpriseID()
	if item.EnterpriseID == "" {
		return ctx.ResErrBadRequest(fmt.Errorf("请先选择企业"))
	}

	item.Creator = ctx.GetUserID()

	err := a.AuthCodeBll.Create(&item)
	if err != nil {
		return ctx.ResErrInternalServer(err)
	}

	data := map[string]interface{}{
		"status": "ok",
		"code":   item.Code,
	}
	return ctx.ResSuccess(data)
}

// Delete 删除数据
func (a *AuthCode) Delete(ctx *context.Context) error {
	ctx.WriteOperateLog("AuthCode.Delete", "认证码管理-删除数据")
	err := a.AuthCodeBll.Delete(ctx.Param("id"))
	if err != nil {
		return ctx.ResErrInternalServer(err)
	}
	return ctx.ResOK()
}

// Disable 作废数据
func (a *AuthCode) Disable(ctx *context.Context) error {
	ctx.WriteOperateLog("AuthCode.Disable", "认证码管理-作废数据")
	err := a.AuthCodeBll.UpdateStatus(ctx.Param("id"), 2)
	if err != nil {
		return ctx.ResErrInternalServer(err)
	}
	return ctx.ResOK()
}
